Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Add/remove style after click label/input[radio] problem

I have a problem with a function that selects active items. There are some input/label fields with sectoins ( one section - one active/clicked element ) It only works when I double-click. If I click one time then no result. I can set setTimeout for that code and then works but not always.

Example:

var radios = document.querySelectorAll('#calculator input[type=radio]'); 
var label = document.querySelectorAll("#calculator label"); 

function active(e) {

    radios.forEach((el,index) => {
        if(el.checked === true) {
            console.log('true');
            label[index].classList.add('active');
        } else {
            console.log('false');
            label[index].classList.remove('active');
        }
    })

}

Example html field:

<input id="p75" type="radio" name="geodesic_size" value="11250" >
<label for="p75">
<input id="p30" type="radio" name="geodesic_size" value="5626" checked>
<label for="p30">
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Not sure if this is what you need, but here's an example of toggling the active class when the checked input changes:

const radios = document.querySelectorAll('input[type="radio"]');
const label = document.querySelectorAll("label"); 

function active(e) {
  radios.forEach((el,index) => {
    if(el.checked === true) {
      console.log('true');
      label[index].classList.add('active');
    } else {
      console.log('false');
      label[index].classList.remove('active');
    }
  })
}

radios.forEach(el => {
  el.addEventListener('change', active);
})
.active {
  color: red;
}
<input id="p75" type="radio" name="geodesic_size" value="11250" >
<label for="p75">First</label>
<input id="p30" type="radio" name="geodesic_size" value="5626" checked>
<label for="p30" class="active">Second</label>

about 4 years ago · Juan Pablo Isaza Report

0

The other answers seem to work fine, I would just add a couple suggestions:

First, you can also access the label(s) associated to an element with HTMLInputElement.labels. Using el.labels[0] would IMO be a bit more robust than relying on the index of the radio button and the label to be the same.

If all you need to do is toggle an active class like this, you could accomplish the same thing with CSS only by using the adjacent sibling combinator (+) and get rid of all the JavaScript:

input:checked + label {
  color: red;
}
<div>
  <input id="p75" type="radio" name="geodesic_size" value="11250">
  <label for="p75">p75</label>
</div>
<div>
  <input id="p30" type="radio" name="geodesic_size" value="5626" checked>
  <label for="p30">p30</label>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

  • There was no major change I have just add <div id="calculator"></div>.

As #calculator input[type=radio] this means select all input with type equals to radio, which are children of id #calculator.

  • Then selected all inputs loop through them attach onclick event and put you logic for active() function there.
  • To see visual effect added .active{background-color:red;} css.

var radios = document.querySelectorAll('#calculator input[type=radio]');
var label = document.querySelectorAll("#calculator label");
radios.forEach(radioBtn => {
  radioBtn.onclick = function() {
    radios.forEach((el, index) => {
      console.log(el.checked);
      if (el.checked === true) {
        console.log('true');
        label[index].classList.add('active');
      } else {
        console.log('false');
        label[index].classList.remove('active');
      }
    })
  };
})
.active {
  background-color: red;
}
<div id="calculator">
  <input id="p75" type="radio" name="geodesic_size" value="11250">
  <label for="p75">p75</label>
  <input id="p30" type="radio" name="geodesic_size" value="5626" checked>
  <label for="p30">p30</label>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!